﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

namespace Business.系统设置
{
    public partial class APreason : Form
    {
        public APreason()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void text_neirong_TextChanged(object sender, EventArgs e)
        {

        }

        private void APreason_Load(object sender, EventArgs e)
        {
            inicbx(comboBox1, "AP_Item");
            inicbx(comboBox2, "AP_subItem");
        }

        void inicbx(ComboBox cbx, string dataname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select distinct {0} from table_AP", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx.Items.Add(dt.Rows[i][dataname].ToString().Trim());
                if (i == 0)
                    cbx.Text = dt.Rows[0][dataname].ToString().Trim();
            }
        }

        private void lv_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lv.SelectedItems)
            {
                textBox1.Text = item.SubItems[1].Text.ToString().Trim();
                comboBox1.Text = item.SubItems[2].Text.ToString().Trim();
                comboBox2.Text = item.SubItems[3].Text.ToString().Trim();
                text_neirong.Text = item.SubItems[4].Text.ToString().Trim();
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            getjilu();
        }
        public void getjilu()
        {
            lv.Items.Clear();
            if (!(comboBox1.Text.Length > 0) && !(comboBox2.Text.Length > 0))
            {
                MessageBox.Show("项目内容和次级项目内容不可都为空");
                return;
            }
            Query query = new Query();
            string sql = "";
            if (comboBox1.Text.Length > 0)
            {
                sql = string.Format(@"select * from table_AP where AP_Item='{0}'", comboBox1.Text);
            }
            if (comboBox2.Text.Length > 0)
            {
                sql = string.Format(@"select * from table_AP where AP_subItem='{0}'", comboBox2.Text);
            }
            if (comboBox1.Text.Length > 0 && comboBox2.Text.Length > 0)
            {
                sql = string.Format(@"select * from table_AP where AP_Item='{0}'and AP_subItem='{1}'", comboBox1.Text, comboBox2.Text);
            }
            // sql = string.Format(@"select * from table_AP where AP_Item='{0}'and AP_subItem='{1}'", comboBox1.Text, comboBox2.Text);
            DataTable dt = query.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["AP_id"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["AP_Item"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["AP_subItem"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["AP_detail"].ToString().Trim());
                        this.lv.Items.Add(lt);
                    }
                }
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            Query query = new Query();
            string sql = string.Format(@"insert into table_AP (AP_Item_number,AP_Item,AP_subItem_number,AP_subItem,AP_detail_number,AP_detail) values (
0,'{0}',0,'{1}',0,'{2}')", comboBox1.Text, comboBox2.Text, text_neirong.Text);
            query.InsertDB(sql);
            getjilu();
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            Query query = new Query();
            string sql = string.Format(@"update table_AP set AP_Item='{0}',AP_subItem='{1}',AP_detail='{2}' where AP_id={3}", comboBox1.Text, comboBox2.Text, text_neirong.Text,
                textBox1.Text);
            query.InsertDB(sql);
            getjilu();
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            Query query = new Query();
            string sql = string.Format(@"delete table_AP where AP_id={0}",textBox1.Text);
            query.InsertDB(sql);
            getjilu();
        }
    }
}
